﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;

namespace LaKuaShop_TestDemo.Page
{
    /// <summary>
    /// UserPage.xaml 的交互逻辑
    /// </summary>
    public partial class UserPage : System.Windows.Controls.Page
    {
        public UserPage()
        {
            InitializeComponent();
            IniUser();
            Account.DataContext = Common.User;
            Sex.DataContext = Common.User;
        }
        //收货地址
        private void Receive_Click(object sender, RoutedEventArgs e)
        {
            MyReceive w1 = new MyReceive();
            w1.ShowDialog();
        }

        //加载信息
        private void IniUser()
        {
            BLL.User User = new BLL.User();
            Account.Text = Common.User.account;
            Sex.Text = Common.User.sex;
            byte[] img = User.ShowUserIcon(Common.User.account);
            if (img != null)
            {
                MemoryStream stream = new MemoryStream(img);
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();//初始化
                bmp.StreamSource = stream;//设置源
                bmp.EndInit();//初始化结束
                IMG.Source = bmp;
            }
        }

        //更改密码
        private void ChangePass_Click(object sender, RoutedEventArgs e)
        {
            ChangePassword w1 = new ChangePassword();
            w1.ShowDialog();
        }
        //注销按钮
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确认注销?", "确认提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.OK)
            {
                Hard_Cancel();

            }

        }
        private void Hard_Cancel()
        {
            BLL.User User = new BLL.User();
            BLL.BuyCart BuyCart = new BLL.BuyCart();
            BLL.Collect Collect = new BLL.Collect();
            BLL.Order Order = new BLL.Order();

            if ((User.DeleteUserInfo(Common.User.account) == 0) && (Order.DelOrder(Common.User.account) == 0) && (Collect.DelCollect(Common.User.account) == 0) && (BuyCart.DelBuyCart(Common.User.account) == 0) && (User.DeleteUserIcon(Common.User.account) == 0))
            {
                MessageBox.Show("注销成功");
            }
            Common.User = new UserInfo();//清空USER数据
            var window = Window.GetWindow(this);
            window.Close();

        }
        //收藏夹
        private void Collect_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new CollectPage());
        }
        //查看订单按钮
        private void Look_Order_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new LookOrderPage());
        }

        //退出登录
        private void Exit_Button(object sender, RoutedEventArgs e)
        {
            Common.User = new UserInfo();
            Login win = new Login();
            var window = Window.GetWindow(this);
            window.Close();
            win.ShowDialog();
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

            dialog.Filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif|All files(*.*)|*.*";

            if (dialog.ShowDialog() == true)
            {
                //MessageBox.Show(dialog.FileName);              
                if (MessageBox.Show("确认修改头像?", "确认提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    BLL.User User = new BLL.User();
                    byte[] img = File.ReadAllBytes(dialog.FileName);
                    User.ChangeUserIcon(Common.User.account, img);
                    IniUser();
                }
            }
        }

        private void ChangeInfo_Click(object sender, MouseButtonEventArgs e)
        {
            ChangeInfo w1 = new ChangeInfo();
            w1.ShowDialog();
            IniUser();
        }
    }
}
